define(['jquery'], () => {
    class Header {
        constructor() {
            this.loadHTML().then(() => {
                this.search()
                this.calcCartTotal()
            })
        }
        loadHTML() {
            return new Promise(resolve => {
                $('header').load('/html/modules/header.html', resolve)
            })
        }
        // 搜索栏
        search() {
            $('.text').on('keyup', function () {
                $.getJSON('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?cb=?', { wd: $(this).val() }, resp => {
                    const { s } = resp
                    const html = s.reduce((html, item) => {
                        html += `<li>${item}</li>`
                        return html
                    }, '')
                    $('#searchs').html(html)
                })
            })
        }
        calcCartTotal() {
            let count = 0
            let cart = localStorage.getItem('cart')
            if (cart) {
                cart = JSON.parse(cart)
                count = cart.reduce((total, shop) => total + parseInt(shop.count), 0)
            }
            $('.cart-count').html(count)
            $('.zongji span').html(count)
        }
    }
    return new Header()
})